package com.atguigu.b_test;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

/*
    Java --> File
 */
public class Test5 {
    public static void main(String[] args) throws IOException {
        //准备学生对象
        Student stu1 = new Student("guigu005", "西施", 18, "女");
        Student stu2 = new Student("guigu005", "貂蝉", 18, "女");
        Student stu3 = new Student("guigu005", "王昭君", 18, "女");
        Student stu4 = new Student("guigu005", "杨玉环", 18, "女");

        //添加到集合中
        ArrayList<Student> list = new ArrayList<>();
        Collections.addAll(list,stu1,stu2,stu3,stu4);

        //创建输出流对象
        BufferedWriter bw = new BufferedWriter(
                new FileWriter("T_Day23\\test\\StudentInfo.txt",true));

        //遍历集合,并把数据写入到文件中
        for (Student student : list) {
            //取出学生信息
            String sid = student.getSid();
            String name = student.getName();
            int age = student.getAge();
            String gender = student.getGender();
            //guigu004,黎小明,19,男
            StringBuilder sb = new StringBuilder();
            sb.append(sid).append(",")
                    .append(name).append(",")
                    .append(age).append(",")
                    .append(gender);
            //把学生对象信息写到文件中
            bw.write(sb.toString());
            bw.newLine(); //换行
        }


        bw.close();
    }
}
